// pages/auth/auth.js
const util = require('../../utils/util.js')
const app = getApp();
var hopeApi = require('../../vendor/hope-sdk/core/request.js');;
var utils = require('../../vendor/hope-sdk/core/utils.js');;
var config = require('../../config.js');
var srsdkApi = require('../../vendor/hope-sdk/index.js');;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    disabled: false,
    btnText: "获取验证码",
    realname   : '',
    mobile     : "",
    loginCode  : "",
    type: "danger"
  },
  /**
   * getUserinfo回调函数
   */
  onGotUserInfo: function (e) {
    var that = this;
    var data = e.detail;
    if (data.errMsg === "getUserInfo:ok") {
      let realname  = that.data.realname;
      let mobile    = that.data.mobile;
      let loginCode = that.data.loginCode;
      if (util.isNullOrEmpty(realname)){
        util.toast('请输入姓名');
        return
      }else if (util.isNullOrEmpty(mobile)) {
        util.toast('请输入手机号码');
        return
      } else if (!util.isMobile(mobile)) {
        util.toast('请输入正确的手机号码');
        return
      } else if (util.isNullOrEmpty(loginCode)) {
        util.toast('请输入验证码');
        return
      }
      data = utils.extend({}, data, { realname: realname ,mobile: mobile, captcha: loginCode});
      srsdkApi.login.exeAuth(app, 'loginInfo', data).then(function (res) {
        
        // var pages = getCurrentPages();
        // var prevPage = pages[pages.length - 2]; //上一个页面
        // prevPage.setData({
        //   'logined': true,
        //   'userinfo': res,
        //   'authsetting': res.isauth,
        //   'addCarFlag':true
        // }, function () {
        //   wx.navigateBack({
        //     delta: 1
        //   });
        // });


     }).catch(function (error) {
       console.error(error);
       wx.showModal({
         title: 'Error',
         content: error.errMsg,
       })
     });
    } else {
      wx.showModal({ title: 'Warning', content: 'Please permit to authorize.', showCancel: false })
      return false;
    }
  },
  /**
   * 注册
   */
  goAuth:function(e){
    var that = this;
    let realname = that.data.realname;
    let mobile = that.data.mobile;
    let loginCode = that.data.loginCode;
    if (util.isNullOrEmpty(realname)) {
      util.toast('请输入姓名');
      return
    } else if (util.isNullOrEmpty(mobile)) {
      util.toast('请输入手机号码');
      return
    } else if (!util.isMobile(mobile)) {
      util.toast('请输入正确的手机号码');
      return
    } else if (util.isNullOrEmpty(loginCode)) {
      util.toast('请输入验证码');
      return
    }
    var data = { realname: realname, mobile: mobile, captcha: loginCode };
    srsdkApi.login.exeAuth(app, 'loginInfo', data).then(function (res) {
            
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2]; //上一个页面
      prevPage.setData({
        'logined': true,
        'userinfo': res,
        'authsetting': res.isauth,
        'addCarFlag': true
      }, function () {
        wx.navigateBack({
          delta: 1
        });
      });

    }).catch(function (error) {
      console.error(error);
      wx.showModal({
        title: '提示',
        content: error.errMsg,
      })
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  inputRealname: function (e) {
    this.setData({
      realname: e.detail.value
    });
  },
  inputMobile: function (e) {
    this.setData({
      mobile: e.detail.value
    });
  },
  inputLoginCode:function(e){
    this.setData({
      loginCode: e.detail.value
    });
  },
  doLoop: function (seconds) {
    seconds = seconds ? seconds : 60;
    this.setData({
      btnText: seconds + "s后获取",
    });
    let countdown = setInterval(() => {
      if (seconds > 0) {
        this.setData({
          btnText: seconds + "s后获取"
        });
        --seconds;
      } else {
        this.setData({
          btnText: "获取验证码",
          disabled: false,
          type: "danger"
        });
        clearInterval(countdown);
      }
    }, 1000);
  },
  btnSend: function () {
    var that = this;
    if (util.isNullOrEmpty(this.data.mobile)) {
      util.toast('请输入手机号码');
      return
    } else if (!util.isMobile(this.data.mobile)) {
      util.toast('请输入正确的手机号码');
      return
    }
    this.setData({
      disabled: true,
      btnText: "请稍候...",
      type: "danger"
    });

    hopeApi.request(app,{
      url: config.service.sendSms,
      login: false,
      data: {
        event: 'register',
        mobile: that.data.mobile
      },
      success: function (res) {
        setTimeout(() => {
          that.doLoop(60)
        }, 500)
        util.toast('发送成功');
      },
      fail: function (res) { 
        that.setData({
          btnText: "获取验证码",
          disabled: false,
          type: "danger"
        });
        util.toast(res);
      },
    })
  },
  login: function (e) {
    let loginCode = e.detail.value.smsCode;
    let mobile    = e.detail.value.mobile;
    if (util.isNullOrEmpty(mobile)) {
      util.toast('请输入手机号码');
      return
    } else if (!util.isMobile(mobile)) {
      util.toast('请输入正确的手机号码');
      return
    } else if (util.isNullOrEmpty(loginCode)) {
      util.toast('请输入验证码');
      return
    }
    xrsdk.request(app,{
      url: config.service.registerUrl,
      login: true,
      data: {
        captcha: loginCode,
        mobile: mobile
      }
    }).then(res => {
      app.globalData.logined = true;
      app.globalData.userinfo = res.userinfo;
      util.toast("登录成功", 2000, true);
      wx.navigateBack({
        delta: 1
      });
    }).catch(res => {
      util.toast(res.errMsg, 2000, true);
    });
  },
  goHome: function () {
    wx.switchTab({
      url: "/pages/home/home"
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})